import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import refresh from '@dfeidao/atom-web/url/refresh';

export default async function a002(fd: IFeidaoAiBrowserComponent) {/*修改保存 */
	// todo
	console.log('zj-000005:a002----start');

	// 判断隐患源特性代码
	const hc = fd.data.node.querySelector<HTMLInputElement>('[data-id="hidden_source_feature_code"]').value;
	if (!hc) {
		alert('请完善信息');
		return;
	}
	// 判断隐患源特性名称
	const hn = fd.data.node.querySelector<HTMLInputElement>('[data-id="hidden_source_feature_name"]').value;
	if (!hn) {
		alert('请完善信息');
		return;
	}
	// 判断控制数据
	const cd = fd.data.node.querySelector<HTMLSelectElement>('[data-id="control_data"]');
	const cdv = cd.value;
	if (!cdv) {
		alert('请完善信息');
		return;
	}
	console.log('aaa');
	// 判断是否存在隐患源特性代码
	const no = get(fd, 'no') as string;// 获取当前数据的隐患源编号
	const res = await nodejs<Array<{ hidden_source_feature_no: string }>>('wyf/zj-000004/s001', {
		hidden_source_feature_code: hc
	});
	console.log(res);
	let flag = true;
	if (res.length > 0) {
		for (let i = 0; i < res.length; i++) {
			if (res[i].hidden_source_feature_no !== no) {
				flag = false;
			}
		}
	}
	if (!flag) {
		alert('特性代码已存在');
	} else {
		const res1 = await nodejs<{ code: number; result: string }>('wyf/zj-000005/s002', {
			filter: {
				hidden_source_feature_no: no,
				hidden_source_feature_name: hn,
				hidden_source_feature_code: hc,
				control_data: cdv
			}
		});
		if (res1.code === 1) {
			alert('保存成功');
			refresh();
		} else {
			alert('保存失败');
		}

	}

	console.log('zj-000005:a002----end');
}
